<?php
/**
 * Created by PhpStorm.
 * User: davin
 * Date: 14-10-19
 * Time: 下午9:25
 */


class MobileAccountService extends ServiceBase {


    public function getAccountLogList($member, $inputs = array(), $pageCount = null){

        //允许排序的字段名称数组
        $canOrderCols = array('name', 'created_at', 'operate_type', 'account_type', 'happen_money', 'current_money', 'order_id');

        $order = Input::get( 'order' );
        $reverse = Input::get( 'reverse',0 );
        $page = Input::get( 'page', 1 );

        $entries = MemberAccountLog::where('member_id', $member->id)->orderby(in_array($order, $canOrderCols)?$order:'updated_at', $reverse == 0?'desc':'asc');

        $entries = $entries->paginate(Config::get('app.pagenate_num'))
            ->addQuery('order',$order)
            ->addQuery('reverse',$reverse);

        $links = array(
            "order"=>$order,
            "reverse"=>$reverse,
            "page"=>$page,
        );

        return array('entries'=>$entries, 'links'=>$links);
    }

    public function getAccount($member, $accountType = self::INTEGRAL_ACCOUNT){
        $memberAccount = new MemberAccount();
        $account = $memberAccount->getAccount($member->id, $accountType);
        if(!$account){
            $this->errors()->add('error', '会员账户开通失败！');
            return false;
        }
        return $account;
    }


    private function getSearchField($inputs, $key, $default = null){
        return isset($inputs[$key]) ? $inputs[$key] : Input::get($key, $default);
    }

}

